<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <input type="text" placeholder="请输入数字" id="num">
    <input type="button" value="阶乘运算" id="btn">
    <div id="box"></div>
</body>
<script>
    var btn = document.getElementById('btn');
    function jiecheng(n1) {//封装函数
        var total = 1;//引入循环
        for (var j = 1; j <= n1; j++) {
            total *= j;
        }
        return total;
    }
    btn.onclick = function () {//定义匿名函数
        var num = document.getElementById('num').value * 1;
        var box = document.getElementById('box');
        var a = jiecheng(num);//调用阶乘函数
        box.innerHTML = a;
    }
</script>

</html>